import React from 'react';
import { Text, View, ToastAndroid, StatusBar, StyleSheet } from 'react-native';
import px from '../utils/px';
import BackPressComponent from '../components/base/BackPress';
import GlobalStyles from '../style/GlobalStyles';
import NavigationUtil from '../navigation/NavigationUtil';

export default class HomeScreen extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
        NavigationUtil.navigation = this.props.navigation;
        this.backPress = new BackPressComponent({ backPress: () => this.onBackPress() });
    }

    static navigationOptions = {
        title: '首页',
        header: null
    };

    componentDidMount() {
        NavigationUtil.willFocus(() => {
            this.backPress.componentDidMount();
        });
        NavigationUtil.willBlur(() => {
            this.backPress.componentWillUnmount();
        });
    }

    // 监听安卓返回键
    onBackPress = () => {
        // 最近2秒内按过back键，可以退出应用。
        if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) return false;
        this.lastBackPressed = Date.now();
        ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
        return true;
    };

    render() {
        return (
            <View style={styles.container}>
                <StatusBar
                    backgroundColor={GlobalStyles.MAIN_COLOR}/>
                <View style={styles.backHeader}></View>
                <Text>首页</Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        display: 'flex',
        flex: 1,
        width: GlobalStyles.DEVICE_WIDTH,
        height: GlobalStyles.DEVICE_HEIGHT
    },
    backHeader: {
        width: '100%',
        height: px(180),
        backgroundColor: GlobalStyles.MAIN_COLOR
    }
});
